package net.huashitong.until;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dataBean.CodeDataBean;
import net.huashitong.Indicators.entity.IndexData;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class Until {

    private static Logger log = LoggerFactory.getLogger(Until.class);

    public static final String DANGQIZHI = "SGB0101";
    public static final String LEIJIZHI = "SGB0301";
    public static final String LEIJIZENGSU = "SGB0303";
    public static final String QIMOZHI = "SGB0201";
    public static final String TONGBIZZ = "SGB0103";
    public static final String BI_SHAGN_NIAN = "SGB0203";


    public static final String XMDB_YUEDU = "hgyd2";//厦门市月度数据库
    public static final String XMDB_JIDU = "xmjd";//厦门季度数据库
    public static final String XMDB_NIANDU = "xmnd2";//厦门年度数据库
    public static final String NIANDU_DB = "qyfjnd2";  //省市年度数据 厦门市，福州市，泉州市...。

    public static final String FUSHENGJI_15 = "appfsjyd"; //  十五个副省级
    public static final String JINGJITEQU_4 = "appjjtqyd"; // 四个经济特区
    public static final String QUANGUO = "appqgyd"; // 全国
    public static final String QUJI_6 = "appqjyd";    //区级
    public static final String JIUSHIJI_9 = "appsjyd"; // 市级

    public static final String JIDU_9DI = "app9sjd";// 	9个设区市季度数据（APP）
    public static final String NIANDU_9DI = "app9snd";//  9个设区市年度数据（APP）
    public static final String YUEDU_9DI = "app9syd";//  9个设区市月度数据（APP）
    public static final String JIDU_FUSHENGJI15 = "appfsjjd";//   十五个副省级季度数据（APP）
    public static final String NIANDU_FUSHENGJI15 = "appfsjnd";//   十五个副省级年度数据（APP）
    public static final String YUEDU_FUSHENGJI15 = "appfsjyd";//   十五个副省级月度数据（APP）
    public static final String JIDU_TEQU4 = "appjjtqjd";//    四个经济特区季度数据（APP）
    public static final String NIANDU_TEQU4 = "appjjtqnd";//    四个经济特区年度数据（APP）
    public static final String YUEDU_TEQU4 = "appjjtqyd";//    四个经济特区月度数据（APP）
    public static final String JIDU_QUANGUO = "appqgjd";//  全国季度数据（APP）
    public static final String NIANDU_QUANGUO = "appqgnd";//  全国年度数据（APP）
    public static final String YUEDU_QUANGUO = "appqgyd";//  全国月度数据（APP）
    public static final String JIDU_QUJI = "appqjjd";//  区级季度数据（APP）
    public static final String NIANDU_QUJI = "appqjnd";//  区级年度数据（APP）
    public static final String YUEDU_QUJI = "appqjyd";//  区级月度数据（APP）
    public static final String YEUDU_SHOUYE = "appshouye";//    首页月度数据（APP）
    public static final String JIDU_SHIJI = "appsjjd";//  市级季度数据（APP）
    public static final String NIANDU_SHIJI = "appsjnd";//  市级年度数据（APP）
    public static final String YUEDU_SHIJI = "appsjyd";//  市级月度数据（APP）
    //全省
    public static final String YUEDU_QS = "appqsyd";//  全省月度数据（APP）
    public static final String JIDU_QS = "appqsjd";//  全省季度数据（APP）
    public static final String NIANDU_QS = "appqsnd";//  全省年度数据（APP）


    //=====================================外网=====================================

    /*public static final String UPDATE_URL = "http://211.103.185.201:20123/pub/app_2/dataupdate";
    //热词
    public static final String HOT_WORDS_URL = "http://211.103.185.201:20123/pub/app_2/getHotWords";
    //热词需要token
    public static final String HOT_WORDS_URL_NEED_TOKEN = "http://211.103.185.201:20123/pub/app_2/getHotWords1?";
    //获取指标ID
    public static final String Ind_CODE_NAME_DB_URL = "http://211.103.185.201:20123/pub/app_2/getdatatreenodes?";
    //登录接口
    public static final String LOGIN_URL = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/checklogin?";
    //地区1  http://211.103.185.201:20123/pub/app_2/getdatasubreg?
    public static final String AREA_CODE_URL_DB_URL = "http://211.103.185.201:20123/pub/app_2/getdatasubreg?";
    //获取数据
    public static final String DATA_QUERY_URL_DB_URL = "http://211.103.185.201:20123/pub/app_2/dataquery?";
    //时间类型
    public static final String TIME_TYPE = "http://211.103.185.201:20123/pub/app_2/getalltimetype?db=appnd2&p=SGB01";
    //统计分析-获取AccessToken接口
    public static final String GET_ACCESS_TOKEN = "http://cloud.xm.gov.cn/165/service/getAccessToken.xhtml?";
    //获取指定父栏目的所有子栏目接口
    public static final String GET_CHS = "http://cloud.xm.gov.cn/165/service/getChannels.xhtml";
    //获取指定栏目接口
    public static final String GET_CH = "http://cloud.xm.gov.cn/165/service/getChannel.xhtml";
    //获取文章列表接口
    public static final String GET_DOCUMENTS = "http://cloud.xm.gov.cn/165/service/getDocuments.xhtml?";
    //获取指定文章接口
    public static final String GET_ARTICLE = "http://cloud.xm.gov.cn/165/service/getDocument.xhtml?";
    //获取用户列表(需要token)
    public static final String GET_USERS_NEED_TOKEN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getUserList1?token=";
    //获取用户列表(不需要token)
    public static final String GET_USERS = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getUserList";
    //App接口登录 内置用户  用户名：acmr 密码：acmr123 登录成功得到一个token作为其他接口调用的凭证 有效期为1天
    public static final String GET_TOKEN_DEFAULT = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/appLogin?user=acmr&pwd=acmr123";
    //获取token（需要传入用户名跟密码）
    public static final String GET_TOKEN_LOGIN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/appLogin?";

    //获取消息列表(需要token)
    public static final String GET_MYINFO_NEED_TOKEN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getmyinfolist1?";
    //获取消息列表(不需要token)
    public static final String GET_MYINFO = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getmyinfolist?";
    //获取单条消息(需要token)
    public static final String GET_ONE_INFO_NEED_TOKEN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getOneInfo1?";
    //获取单条消息(不需要token)
    public static final String GET_ONE_INFO = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getOneInfo?";
    //删除我的消息(需要token)
    public static final String DELETE_INFO_NEED_TOKEN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/deleteMyInfo1?";
    //删除我的消息(不需要token)
    public static final String DELETE_INFO = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/deleteMyInfo?";
    //消息已读(需要token)
    public static final String LOOK_INFO_NEED_TOKEN = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/lookInfo1?";
    //消息已读(不需要token)
    public static final String LOOK_INFO = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/lookInfo?";
    //获取用户指定查询库没有权限的指标列表(需要token)
    public static final String GET_NORIGHTLIST_NEED_TOKEN = "http://211.103.185.201:20123/pub/app_2/getNoRightList1?db=xmnd&";
    //获取用户指定查询库没有权限的指标列表(不需要token)
    public static final String GET_NORIGHTLIST = "http://211.103.185.201:20123/pub/app_2/getNoRightList?db=xmnd&";

    //————————————————————————————————————————————————————————————————————————————————————————————————
    //App是否需要安全验证
    public static final String IF_APP_VALIDATE = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/ifAPPValidate?";
    //获取用户无查看权限的APP栏目列表
    public static final String GET_NO_RIGHT = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getNoRightAppMenus?";
    //获取APP栏目列表
    public static final String GET_APP_MENUS = "http://211.103.185.201:20123/acmrxminput/api/XmInputApi/getAppMenus";
    //重置密码接口
    public static final String RESET_PWD = "http://211.103.185.201:20123/acmrpassport/api/chongzhiApi/chongzhi?";
    //激活接口
    public static final String JI_HUO = "http://211.103.185.201:20123/acmrpassport/api/jihuoApi/jihuo?";
    //验证手机号码是否是预制用户
    public static final String JUDGE_USER = "http://211.103.185.201:20123/acmrpassport/api/jihuoApi/judgeUser?";
    //获取用户没有权限的菜单列表
    public static final String HAVE_UN_POWER = "http://211.103.185.201:20123/acmrxminput/api/FuncAppApi/haveUnPower?";
    //查询时间
    public static final String GET_DATA_TIME = "http://211.103.185.201:20123/pub/app_2/getdataallsj?";
    //获取指标详情
    public static final String GET_DATA_INFO = "http://211.103.185.201:20123/pub/app_2/getdatanode?wd=zb";*/

    //=====================================内网=====================================

    public static final String UPDATE_URL = "http://10.23.193.12:81/app_2/dataupdate";
    public static final String HOT_WORDS_URL = "http://10.23.193.12:81/app_2/getHotWords";
    public static final String HOT_WORDS_URL_NEED_TOKEN = "http://10.23.193.12:81/app_2/getHotWords1?";
    public static final String Ind_CODE_NAME_DB_URL = "http://10.23.193.12:81/app_2/getdatatreenodes?";
    public static final String LOGIN_URL = "http://10.23.193.12/api/XmInputApi/checklogin?";
    public static final String AREA_CODE_URL_DB_URL = "http://10.23.193.12:81/app_2/getdatasubreg?";
    public static final String DATA_QUERY_URL_DB_URL = "http://10.23.193.12:81/app_2/dataquery?";
    public static final String TIME_TYPE = "http://10.23.193.12:81/app_2/getalltimetype?db=appnd2&p=SGB01";
    public static final String GET_ACCESS_TOKEN = "http://cloud.xm.gov.cn/165/service/getAccessToken.xhtml?";
    public static final String GET_CHS = "https://cloud.xm.gov.cn/165/service/getChannels.xhtml";
    public static final String GET_CH = "https://cloud.xm.gov.cn/165/service/getChannel.xhtml";
    public static final String GET_DOCUMENTS = "http://cloud.xm.gov.cn/165/service/getDocuments.xhtml?";
    public static final String GET_ARTICLE = "http://cloud.xm.gov.cn/165/service/getDocument.xhtml?";
    public static final String GET_USERS_NEED_TOKEN = "http://10.23.193.12/api/XmInputApi/getUserList1?token=";
    public static final String GET_USERS = "http://10.23.193.12/api/XmInputApi/getUserList";
    public static final String GET_TOKEN_DEFAULT = "http://10.23.193.12/api/XmInputApi/appLogin?user=acmr&pwd=acmr123";
    public static final String GET_TOKEN_LOGIN = "http://10.23.193.12/api/XmInputApi/appLogin?";
    public static final String GET_MYINFO_NEED_TOKEN = "http://10.23.193.12/api/XmInputApi/getmyinfolist1?";
    public static final String GET_MYINFO = "http://10.23.193.12/api/XmInputApi/getmyinfolist?";
    public static final String GET_ONE_INFO_NEED_TOKEN = "http://10.23.193.12/api/XmInputApi/getOneInfo1?";
    public static final String GET_ONE_INFO = "http://10.23.193.12/api/XmInputApi/getOneInfo?";
    public static final String DELETE_INFO_NEED_TOKEN = "http://10.23.193.12/api/XmInputApi/deleteMyInfo1?";
    public static final String DELETE_INFO = "http://10.23.193.12/api/XmInputApi/deleteMyInfo?";
    public static final String LOOK_INFO_NEED_TOKEN = "http://10.23.193.12/api/XmInputApi/lookInfo1?";
    public static final String LOOK_INFO = "http://10.23.193.12/api/XmInputApi/lookInfo?";
    public static final String GET_NORIGHTLIST_NEED_TOKEN = "http://10.23.193.12:81/app_2/getNoRightList1?db=xmnd&";
    public static final String GET_NORIGHTLIST = "http://10.23.193.12:81/app_2/getNoRightList?db=xmnd&";
    public static final String IF_APP_VALIDATE = "http://10.23.193.12/api/XmInputApi/ifAPPValidate?";
    public static final String GET_NO_RIGHT = "http://10.23.193.12/api/XmInputApi/getNoRightAppMenus?";
    //菜单权限接口
//    public static final String GET_NO_RIGHT = "http://10.23.193.12/api/FuncAppApi/haveUnPower?";

    public static final String GET_APP_MENUS = "http://10.23.193.12/api/XmInputApi/getAppMenus";
    public static final String RESET_PWD = "http://10.23.193.12/acmrpassport/api/chongzhiApi/chongzhi?";
    public static final String JI_HUO = "http://10.23.193.12/acmrpassport/api/jihuoApi/jihuo?";
    public static final String JUDGE_USER = "http://10.23.193.12/acmrpassport/api/jihuoApi/judgeUser?";
    public static final String GET_DATA_TIME = "http://10.23.193.12:81/app_2/getdataallsj?";
    public static final String GET_DATA_INFO = "http://10.23.193.12:81/app_2/getdatanode?wd=zb";
    //获取用户没有权限的菜单列表
    public static final String HAVE_UN_POWER = "http://10.23.193.12/api/FuncAppApi/haveUnPower?";

    //=====================================内网=====================================


    /**
     * httpGet请求
     *
     * @param url
     * @param SocketTimeout
     * @param ConnectTimeout
     * @return
     */
    public static String HttpGET(String url, int SocketTimeout, int ConnectTimeout) throws IOException {
        String response = null;
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet httpget = new HttpGet(url);
        RequestConfig reqConfig = RequestConfig.custom().setSocketTimeout(SocketTimeout).setConnectTimeout(ConnectTimeout).build();
        httpget.setConfig(reqConfig);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        response = client.execute(httpget, responseHandler);
        return response;
    }

    /**
     * 需要分割的字符
     *
     * @param str
     */
    public static CodeDataBean spitString(String str) {
        List<String> list = new ArrayList<>();
        CodeDataBean bean = new CodeDataBean();
        String[] index = str.split("_");
        for (int i = 0; i < index.length; i++) {
            //list.add( index[i].toString().substring(3) );
            if (index[i].toString().contains("zb.")) {
                bean.setCodeId(index[i].toString().substring(3));
            }
            if (index[i].toString().contains("sj.")) {
                bean.setDatetime(index[i].toString().substring(3));
            }
            if (index[i].toString().contains("tt.")) {
                bean.setTimetype(index[i].toString().substring(3));
            }
            if (index[i].toString().contains("reg.")) {
                bean.setAreaCode(index[i].toString().substring(4));
            }

        }

        return bean;
    }

    //获取当前日期时间
    public static String getCurrentDateTime() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    //获取当前月份
    public static String getCurrentMonth() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String currnetDateTime = sf.format(date);
        return currnetDateTime;
    }

    //获取年份
    public static String getCurrenYear() {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy");
        String currentYear = sf.format(date);
        return currentYear;
    }

    /**
     * 时间类型
     */
    public static List<String> TimeTypes() {
        //当期值、累计值、累计比上年同期增长率、期末值、比上年同期增长率
        //年度指教参数：SGB0203改成SGB0103
        List<String> sList = Arrays.asList("SGB0101", "SGB0301", "SGB0303", "SGB0201","SGB0103","SGB0203");
        return sList;
    }

    /**
     * 去空list 保留非空
     */
    public static List<List<IndexData>> remainList(List<IndexData> list1, List<IndexData> list2, List<IndexData> list3) {
        List<List<IndexData>> list = new ArrayList();
        list.add(list1);
        list.add(list2);
        list.add(list3);
        CopyOnWriteArrayList<List<IndexData>> copy = new CopyOnWriteArrayList(list);
        for (List<IndexData> item : copy) {
            if (ObjectUtils.isBlank(item)) {
                copy.remove(item);
            }
        }
        return copy;

    }

    public static String chartToMeam(String str) {
        if ("A".equals(str)) {
            return "第一季度";
        }
        if ("B".equals(str)) {
            return "第二季度";
        }
        if ("C".equals(str)) {
            return "第三季度";
        }
        if ("D".equals(str)) {
            return "第四季度";
        }
        return str;
    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr = str.replace("-", "");
        return uuidStr;
    }


    /**
     * List 深度复制   List复制器
     *
     * @param src
     * @param <T>
     * @return copylist
     */
    public static <T> List<T> deepCopy(List<T> src) {
        try {
            ByteArrayOutputStream byteout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteout);
            out.writeObject(src);
            ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bytein);
            @SuppressWarnings("unchecked")
            List<T> dest = (List<T>) in.readObject();
            return dest;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取数据库
     *
     * @param codeType
     * @param areaYtpe
     * @return
     */
    public static String getDBName(String codeType, String areaYtpe) {
        String db = "";
        //全国
        if ("G".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_QUANGUO;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_QUANGUO;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_QUANGUO;
            }
        }
        //九地市
        if ("E".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_9DI;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_9DI;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_9DI;
            }
        }
        //区级
        if ("A".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_QUJI;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_QUJI;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_QUJI;
            }
        }
        //四个特区
        if ("B".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_TEQU4;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_TEQU4;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_TEQU4;
            }
        }
        //十五个副省级
        if ("C".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_FUSHENGJI15;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_FUSHENGJI15;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_FUSHENGJI15;
            }
        }
        //十五个副省级
        if ("D".equals(areaYtpe)) {
            if ("Y".equals(codeType)) {
                db = Until.YUEDU_SHIJI;
            } else if ("J".equals(codeType)) {
                db = Until.JIDU_SHIJI;
            } else if ("N".equals(codeType)) {
                db = Until.NIANDU_SHIJI;
            }
        }
        return db;
    }

    //获取单位
    //获取单位
    public static Map<String, String> getUnit(String db, String codeId) {
        Map<String, String> resMap = new HashMap<>();
        String unitname = "";
        String exp = "暂无解释";
        resMap.put("unitname", unitname);
        resMap.put("exp", exp);
        JSONObject indexInfo = null;
        String response = null;
        try {
            response = Until.HttpGET(Until.GET_DATA_INFO + "&db=" + db + "&code=" + codeId, 24000, 24000);
            JSONObject obj = JSONObject.parseObject(response);
            int returncode = obj.getIntValue("returncode");
            if (200 == returncode) {
                indexInfo = obj.getJSONObject("returndata");
                unitname = indexInfo.getString("unitname");
                exp = indexInfo.getString("exp");
                resMap.put("unitname", unitname);
                if (StringUtils.isBlank(exp)) {
                    resMap.put("exp", "暂无解释");
                } else {
                    resMap.put("exp", exp);
                }
            }
            return resMap;
        } catch (IOException e) {
            log.info("接口：" + Until.GET_DATA_INFO + "&db=" + db + "&code=" + codeId);
        }
        return resMap;
    }

}
